import { Home } from '../home/home-model.js';
var home = new Home();
import { Video } from '../video/model.js';
var video = new Video();
Page({

  data: {
    tabs:[
      { name: '进行中',status: 1 },
      { name: '未开始',status: 2 },
      { name: '已结束',status: 3 }
    ],
    tabIndex:0,
    page:1,
    rooms:[],
    hiddenNomore: true,
    hiddenLoading: true,
    hiddenNullData: true,
    onLoading: false,
    nomore: false,
  },

  onLoad: function (options) {
    var that = this;
    //获得bannar信息
    var params = { 'num': 3, 'key': 'zhibo' };
    home.adv(params,(data) => {
      that.setData({
        bannerArr: data.items,
      });
    });
    this.videoList();
  },
  tabChange:function(e){
    if (this.data.onLoading){
      return false;
    }
    var index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex : index
    })
    this.reset();
    this.videoList();
  },
  reset:function(){
    this.data.page =1;
    this.setData({
      rooms: []
    })
  },
  videoList:function(){
    var that = this;
    var params={
      page : this.data.page
    }
    if (this.data.onLoading) {
      return false;
    }
    that.data.onLoading = true;
    video.lists(params, (data) => {
      that.data.onLoading = false;
      var items = data.items;
      items = this.data.rooms.concat(items);
      that.setData({
        rooms: items
      })
      //隐藏正在加载中
      that.setData({
        hiddenLoading: true
      })
      if (items.length == 0) {
        //显示没有数据
        that.setData({
          hiddenNullData: false
        })
      }
      if (that.data.page >= data._meta.pageCount) {
        that.data.nomore = true;
        if (that.data.page > 1) {
          //显示没有更多
          that.setData({
            hiddenNomore: false
          })
        }
      }
    })
  },
  onReady: function () {

  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {

  },

  onReachBottom: function () {
    if (this.data.nomore) {
      return false;
    }
    this.data.page++;
    this.videoList();
  },
  onShareAppMessage: function () {

  }
})